package com.rc.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.rc.activity.R;
import com.rc.activity.dao.tbl_Item;
import com.rc.custom.MyListView;
import com.rc.object.ReceiptObj;
import com.rc.util.Util;

public class AddExpenseAdpter extends BaseAdapter {

	private Context mContext;
	private ArrayList<ReceiptObj> items;
	private ArrayList<tbl_Item> arrtbl_Item;
	private MyListView lvItems;
	private AddExpenseItemsAdpter adpItem;
	private ArrayList<HashMap<Long, ArrayList<tbl_Item>>> hmitem;
	private OnCheckedChangeListener changeListener;

	public AddExpenseAdpter(Context c, ArrayList<ReceiptObj> items,
			ArrayList<HashMap<Long, ArrayList<tbl_Item>>> hmitem,
			OnCheckedChangeListener changeListener) {
		mContext = c;
		this.items = items;
		this.changeListener = changeListener;
		this.hmitem = hmitem;
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	private View v;

	// create a new view for each item referenced by the Adapter
	public View getView(final int position, View convertView, ViewGroup parent) {
		v = convertView;
		TextView txtMerchantName, txtAmount, txtDate, txtReceiptType;
		{ // if it's not recycled, initialize some
			// attributes
			LayoutInflater vi = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.trip_add_expense_item_group, null);
		}
		txtMerchantName = (TextView) v.findViewById(R.id.txtMerchantName);
		txtAmount = (TextView) v.findViewById(R.id.txtAmount);
		txtReceiptType = (TextView) v.findViewById(R.id.txtReceiptType);
		txtDate = (TextView) v.findViewById(R.id.txtDate);
		lvItems = (MyListView) v.findViewById(R.id.lvItems);

		txtMerchantName.setText(items.get(position).getMerchantName());
		txtAmount.setText(items.get(position).getTotal());
		txtDate.setText(items.get(position).getDateTime());

		// convert receiptType to text
		String receiptType = Util.getReceiptTypeByInt(mContext,
				Integer.valueOf(items.get(position).getReceiptType()));
		txtReceiptType.setText(receiptType);

		arrtbl_Item = new ArrayList<tbl_Item>();
		arrtbl_Item = hmitem.get(position).get(
				items.get(position).getReceiptID());

		adpItem = new AddExpenseItemsAdpter(mContext, arrtbl_Item,
				changeListener);
		lvItems.setAdapter(adpItem);

		return v;
	}

	public void NotiDB(ArrayList<ReceiptObj> items,
			ArrayList<HashMap<Long, ArrayList<tbl_Item>>> hmitem) {
		this.items = items;
		this.hmitem = hmitem;
		notifyDataSetChanged();
	}
}